﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Xml;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.IO;

namespace fanfu.Common
{
    /// <summary>
    /// XML文件读取器,通常用来读取配置文件,获取指定配置的值
    /// </summary>
    public class XmlReader
    {
        /// <summary>
        /// 读取web.config中SiteConfigFile节点指定的配置文件内容
        /// </summary>
        /// <param name="Target">配置文件中的节点名称</param>
        /// <returns>指定节点的InnerText值</returns>
        public static string GetConfigValue(string Target)
        {
            string path = HttpContext.Current.Server.MapPath("~") + ConfigurationManager.AppSettings["SiteConfigFile"];
            return GetConfigValue(Target, path);
        }
        /// <summary>
        /// 读取配置文件中指定节点的InnerText值
        /// </summary>
        /// <param name="Target">配置文件中的节点名称</param>
        /// <param name="XmlPath">配置文件的路径</param>
        /// <returns>指定节点的InnerText值</returns>
        public static string GetConfigValue(string Target, string XmlPath)
        {
            System.Xml.XmlDocument xdoc = new XmlDocument();
            xdoc.Load(XmlPath);
            XmlElement root = xdoc.DocumentElement;
            XmlNodeList elemList = root.GetElementsByTagName(Target);
            try
            {
                return elemList[0].InnerText;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 读取配置文件中指定节点的子节点，根据code属性值获取第一个匹配的子节点
        /// </summary>
        /// <param name="Target">配置文件中的节点名称</param>
        /// <param name="XmlPath">配置文件的路径</param>
        /// <returns>指定节点的InnerText值</returns>
        public static XmlNodeList GetXmlChildNodes(string Target, string XmlPath)
        {
            System.Xml.XmlDocument xdoc = new XmlDocument();
            xdoc.Load(XmlPath);
            XmlElement root = xdoc.DocumentElement;
            XmlNodeList elemList = root.GetElementsByTagName(Target);
            XmlNode node = null;
            try
            {
                node = elemList[0];
                XmlNodeList childNodeList = node.ChildNodes;
                return childNodeList;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 根据属性值获取节点列表中对应的节点对象
        /// </summary>
        /// <param name="nodeList">节点列表</param>
        /// <param name="attName">属性名称</param>
        /// <param name="attValue">属性值</param>
        /// <returns></returns>
        public static XmlNode GetXmlNodeByAtt(XmlNodeList nodeList, string attName, string attValue)
        {
            XmlNode node = null;
            foreach (XmlNode n in nodeList)
            {
                if (n != null && n.Attributes[attName].Value == attValue)
                {
                    node = n;
                    break;
                }
            }
            return node;
        }

        /// <summary>
        /// 使用xpath获取指定节点的内容
        /// </summary>
        /// <param name="xpath">xpath表达式</param>
        /// <param name="XmlPath">配置文件路径</param>
        /// <returns>指定节点的值</returns>
        public static XmlNode GetSingleNode(string xpath, string XmlPath)
        {
            System.Xml.XmlDocument xdoc = new XmlDocument();
            xdoc.Load(XmlPath);
            XmlElement root = xdoc.DocumentElement;
            return root.SelectSingleNode(xpath);
        }
    }
}
